<?php
require 'init.php';
/*
$sql = "SELECT g.name gname, g.price, i.name iname FROM ".PRE."goods g, ".PRE."image i WHERE g.id = i.goods_id AND i.is_face=1 ORDER BY g.price DESC";
//echo $sql; exit;
$result = mysql_query( $sql );
if ( $result && mysql_num_rows( $result ) > 0 ) {
	$goodslist = array();
	while ( $rows = mysql_fetch_assoc( $result ) ) {
		$goodslist[] = $rows;
	}
}
//shuffle( $goodslist );

$key = array_rand( $goodslist, 4 );

//var_dump( $key );exit;
$n=0;
$num = 9000;
foreach ( $key as $k ){
	$prize_arr[$n]['id'] = $n + 1;
	$img = UPLOAD_URL;
	$img .= substr( $goodslist[$k]['iname'], 0, 4 ) . '/';
	$img .= substr( $goodslist[$k]['iname'], 4, 2 ) . '/';
	$img .= substr( $goodslist[$k]['iname'], 6, 2 ) . '/';
	$img .= $size[2] . 'x' . $size[3] . '_' . $goodslist[$k]['iname'];
	$prize_arr[$n]['prize'] = '<img src="' . $img . '" title="' . $goodslist[$k]['gname'] . '">';
	$prize_arr[$n]['val'] = floor( 1000 / $goodslist[$k]['price'] );
	$num -= $prize[$n]['val'];
	$n++;
} 
$prize_arr[4] = array( 'id' => 5, 'prize' => '10积分', 'val' => '1000');
$prize_arr[5] = array( 'id' => 6, 'prize' => '下次可能就中了', 'val' => $num);

var_dump( $prize_arr );exit;
*/


/*
$prize_arr = array(
    '0' => array('id' => 1, 'prize' => '1', 'v' => 1),
    '1' => array('id' => 2, 'prize' => '2', 'v' => 5),
    '2' => array('id' => 3, 'prize' => '3', 'v' => 10),
    '3' => array('id' => 4, 'prize' => '4', 'v' => 12),
    '4' => array('id' => 5, 'prize' => '5', 'v' => 22),
    '5' => array('id' => 6, 'prize' => '6', 'v' => 50),
);*/
$prize_arr = $_SESSION['prize'];

foreach ($prize_arr as $key => $val) {
    $arr[$val['id']] = $val['v'];
}

$rid = get_rand($arr); //根据概率获取奖项id 

$res['yes'] = $prize_arr[$rid - 1]['prize']; //中奖项 
unset($prize_arr[$rid - 1]); //将中奖项从数组中剔除，剩下未中奖项 
shuffle($prize_arr); //打乱数组顺序 
for ($i = 0; $i < count($prize_arr); $i++) {
    $pr[] = $prize_arr[$i]['prize'];
}
$res['no'] = $pr;
if ( $rid != 5) {//五等奖一增一减不处理
$_SESSION['home']['credits'] -= 10;
$sql = "UPDATE ".PRE."user SET credits = credits - 10 WHERE id = {$_SESSION['home']['id']}";
$result = mysql_query($sql);
}
echo json_encode($res);


